package com.tianqu.tms.www.rental.controller;

import com.ctc.wstx.util.StringUtil;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.RentalLocationInfo;

public class RentalLocationDeletelController extends TmsController {
	private Logger logger = Logger.getLogger(RentalLocationDeletelController.class);
	private int userId = 0;

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		
		TmsUserProfile file = uc.getUserProfile();
		this.userId = file.getUniqueId();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			// ID
			long id = Long.valueOf(uc.getParameter("id"));
			
			RentalLocationInfo rentalLocationInfo = RentalLocationInfo.getInstance();

			// 删除 市场租位信息表（tms_rentalLocation_info）
			rentalLocationInfo.deleteLocationById(id);
			
			// 操作类型
			rentalLocationInfo.setHandleType(3);//3：租位删除
			
			// 备注
			rentalLocationInfo.setRemark(java.net.URLDecoder.decode(uc.getParameter("remark"),"utf8"));
			
			// 更新用户ID
			rentalLocationInfo.setUpdateUserId(userId);
			
			// 租位编号
			rentalLocationInfo.setLocationSerial(uc.getParameter("locationSerial"));
			
			// 插入 租位操作记录表（tms_rental_update_histroy）
			rentalLocationInfo.insertRentalUpdateHistroy(rentalLocationInfo);
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}

